package com.qyhl.party.party.supplement.department;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.party.R;
import com.qyhl.party.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.entity.party.PartyZoneBean;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class PartyDepartmentFragment extends BaseFragment implements PartyDepartmentContract.PartyDepartmentView {
    @BindView(R2.id.search_txt)
    EditText searchTxt;
    @BindView(R2.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private int depId;

    private int level;

    private List<PartyZoneBean> mDataList = new ArrayList<>();

    private CommonAdapter<PartyZoneBean> commonAdapter;

    private String keyword = "";

    private PartyDepartmentPresenter mPresenter;

    public void setLevel(int level) {
        this.level = level;
    }

    public void setDepId(int depId) {
        this.depId = depId;
    }

    public static PartyDepartmentFragment newInstance(int id, int level) {
        PartyDepartmentFragment fragment = new PartyDepartmentFragment();
        fragment.setDepId(id);
        fragment.setLevel(level);
        return fragment;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true,0.2f)
                .statusBarColor(R.color.white)
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.party_fragment_department, null);
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new PartyDepartmentPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(false);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getContext(), R.drawable.party_study_item_divider)));
        recyclerView.addItemDecoration(divider);

        recyclerView.setAdapter(commonAdapter = new CommonAdapter<PartyZoneBean>(getContext(), R.layout.party_item_supplement_zone, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PartyZoneBean partyZoneBean, int position) {
                holder.setText(R.id.name, partyZoneBean.getName());

                if (partyZoneBean.isSelected()) {
                    holder.setTextColorRes(R.id.name, R.color.party_common_red);
                } else {
                    holder.setTextColorRes(R.id.name, R.color.party_common_title);
                }
            }
        });

        mPresenter.getList(level, keyword);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(level, keyword);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(level, keyword);
            }
        });

        searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//按下搜索
                    keyword = searchTxt.getText().toString();
                    mPresenter.getList(level, keyword);
                    hideInput();
                }
                return false;//返回true，保留软键盘。false，隐藏软键盘
            }

        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                for (int i = 0; i < mDataList.size(); i++) {
                    mDataList.get(i).setSelected(i == position);
                }

                if (getActivity() instanceof PartyDepartmentActivity) {
                    ((PartyDepartmentActivity) getActivity()).doFinish(mDataList.get(position).getId(), mDataList.get(position).getName());
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }


    @Override
    public void setList(List<PartyZoneBean> beanList) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        refresh.finishRefresh();

        mDataList.clear();
        mDataList.addAll(beanList);
        for (int i = 0; i < mDataList.size(); i++) {
            mDataList.get(i).setSelected(mDataList.get(i).getId() == depId);
        }
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        loadMask.setErrorText(msg);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        refresh.finishRefresh();
    }

    @OnClick({R2.id.search_btn, R2.id.search_delete})
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.search_btn) {
            keyword = searchTxt.getText().toString();
            mPresenter.getList(level, keyword);
            hideInput();
        } else if (viewId == R.id.search_delete) {
            keyword = "";
            searchTxt.setText(keyword);
            mPresenter.getList(level, keyword);
        }
    }
}
